package org.crag.cafca.gui.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.crag.cafca.db.SqlConnector;
import org.crag.cafca.logic.time.FamilyEvent;
import org.crag.cafca.logic.time.SourceEvent;
import org.crag.cafca.logic.time.Event;
import org.crag.cafca.logic.time.SourceManagementEvent;
import org.crag.cafca.logic.time.TimeLine;
import org.crag.cafca.util.Constants;
import org.crag.cafca.util.Utilities;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Retrieve the data from addTimeEvent.js
 * @author Celien
 *
 */
@WebServlet("/EventServlet")
public class EventServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;

	public EventServlet() 
	{
        super();
    }
	

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{ 
		HttpSession session = request.getSession(); 		
		String mode=(String)request.getParameter("mode");
		String eventType=(String)request.getParameter("eventType");
		TimeLine timeLine=(TimeLine)session.getAttribute("timeline");
		
		if (mode.equals("create"))
		{
			String sourceId=request.getParameter("sourceId");
			String startTimeString=request.getParameter("startTime");
			String endTimeString=request.getParameter("endTime");
			String startDateString=request.getParameter("startDate");
			String endDateString=request.getParameter("endDate");			
			String value=request.getParameter("value");
			String description=request.getParameter("description");
			String tags=request.getParameter("tags");
			
			Event event=null;
			
			if (eventType.equals("SourceEvent"))
			{				
				event=new SourceEvent(sourceId);
			}
			
			if (eventType.equals("SourceManagementEvent"))
			{				
				event=new SourceManagementEvent(sourceId);
			}
			
			if (eventType.equals("FamilyEvent"))
			{				
				event=new FamilyEvent();
			}
			
			if (value!=null)
				event.setValue(value);
			
			if(description!=null)
				event.setDescription(description);

			if (tags!=null)
				event.addTags(tags);
			
			if (startTimeString!=null&&startTimeString.length()!=0&&startDateString!=null&&startDateString.length()!=0)
				event.setStartDateTime(Utilities.parseJSONDateTime(startDateString,startTimeString));

			if (endTimeString!=null&&endTimeString.length()!=0&&endDateString!=null&&endDateString.length()!=0)
				event.setEndDateTime(Utilities.parseJSONDateTime(endDateString,endTimeString));

			
			timeLine.addEvent(event);
			
			SqlConnector sqlConnector = new SqlConnector();
			sqlConnector.saveNewEvent(event);
			sqlConnector.close();	
		}
		
		if (mode.equals("change"))
		{			
			String eventIdString=request.getParameter("id");
			String sourceId=request.getParameter("sourceId");
			String startTimeString=request.getParameter("startTime");
			String endTimeString=request.getParameter("endTime");
			String startDateString=request.getParameter("startDate");
			String endDateString=request.getParameter("endDate");	
			String value=request.getParameter("value");
			String description=request.getParameter("description");
			String tags=request.getParameter("tags");
			
			SourceEvent event=(SourceEvent)timeLine.getEventFromId(Integer.parseInt(eventIdString));
			
			if (sourceId!=null)
				event.setSourceId(sourceId);
			
			if (startTimeString!=null)
				event.setStartDateTime(Utilities.parseJSONDateTime(startDateString, startTimeString));

			if (endTimeString!=null)
				event.setEndDateTime(Utilities.parseJSONDateTime(endDateString, endTimeString));
			
			if (value!=null)
				event.setValue(value);
			
			if(description!=null)
				event.setDescription(description);

			if(tags!=null)
			{
				event.clearTags();
				event.addTags(tags);
			}
			
			SqlConnector sqlConnector = new SqlConnector();
			sqlConnector.updateEvent(event);
			sqlConnector.close();	
		}
		
		if (mode.equals("delete"))
		{
			String eventIdString=request.getParameter("id");
			
			SourceEvent event=(SourceEvent)timeLine.getEventFromId(Integer.parseInt(eventIdString));
			timeLine.deleteEvent(event);
			
			SqlConnector sqlConnector = new SqlConnector();
			sqlConnector.deleteEvent(event);
			sqlConnector.close();	
		}
	}
		
	/**
	 * Return a HttpResponse so that the user has a summary on what he added as a source event
	 * @param sourceEvent
	 * @param dateEvent
	 * @param sourceType
	 * @param response
	 */	
	private void sendResponse(HttpServletResponse response,JSONObject resultJson,boolean success) throws IOException, JSONException 
	{
		resultJson.put(Constants.JSON_SUCCESS, success);
		response.getWriter().write(resultJson.toString());
		response.setStatus(HttpServletResponse.SC_OK);
	}
}
